// const express=require("express")

// const app=express()
// app.use(express.static("./public"))
// app.get("/",(req,res)=>{
//     res.send({ok:1})
// })

//websocket响应
const WebSocket= require("ws") ;
const WebSocketServer =WebSocket.WebSocketServer
//设置端口
const wss = new WebSocketServer({ port: 8000 });

//开启连接
wss.on('connection', function connection(ws) {

  //响应连接方
  ws.on('message', function message(data) {
    console.log('received: %s', data);
    //发送给其他所有聊天用户
    wss.clients.forEach(function each(client) {
        //发给除了自己的所有已连接的用户
        if (client !== ws && client.readyState === WebSocket.OPEN) {
       
          client.send(data,{binary:false});
        }
      });
  });

  ws.send('欢迎来到聊天室');
});
// app.listen(3000)